const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentId: '1',
    section: [{
      name: '昨日',
      typeId: '1'
    }, {
      name: '今日',
      typeId: '2'
    }],
    team_id: '', // 小组ID
    yesterdayStartTime: '', // 昨日开始时间
    yesterdayEndTime: '', // 昨日结束时间
    todayStartTime: '', // 今日开始时间
    todayEndTime: '', // 今日结束时间
    yesterdayData: [], // 昨日打卡数据
    todayData: [], // 今日打卡数据
    fontSetSize: 'small', // 用户设置的字体大小（'small'：小，'middle'：中，'large'：大）
  },

  /**
   * 切换区间
   * @param {*} e 
   */
  handleTap: function(e) {
    let id = e.currentTarget.id
    if (id) {
      this.setData({
        currentId:id
      })
    }
  },

  /**
   * 获取昨日打卡统计
   */
  getYesterdayPunchTheClock () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/team_study_sort',
        data: {
          token,
          team_id: this.data.team_id,
          start_time: this.data.yesterdayStartTime,
          end_time: this.data.yesterdayEndTime
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'yesterdayRes');
      let yesterdayData = res.data.data
      let team_user = yesterdayData.team_user
      team_user.forEach(item => {
        let created_at = item.created_at ? item.created_at.substr(0, 10) : item.created_at
        item.created_at = created_at
      })
      yesterdayData.team_user = team_user
      console.log(yesterdayData)
      this.setData({
        yesterdayData
      })
    }).catch(err => {
      console.log(err, 'yesterdayErr')
    })
  },

  /**
   * 获取本月打卡统计
   */
  getTodayPunchTheClock () {
    let token = wx.getStorageSync('token')
    app.http({
      config: {
        url: '/api/User/team_study_sort',
        data: {
          token,
          team_id: this.data.team_id,
          start_time: this.data.todayStartTime,
          end_time: this.data.todayEndTime
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'todayRes');
      let todayData = res.data.data
      let team_user = todayData.team_user
      team_user.forEach(item => {
        let created_at = item.created_at ? item.created_at.substr(0, 10) : item.created_at
        item.created_at = created_at
      })
      todayData.team_user = team_user
      console.log(todayData)
      this.setData({
        todayData
      })
    }).catch(err => {
      console.log(err, 'todayErr')
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let team_id = options.team_id
    let team_name = options.team_name

    this.setData({
      team_id
    })

    app.setTitle(team_name)

    let timeStamp = new Date().getTime() // 当前时间戳

    let yesterdayStartDate = new Date((timeStamp - (1000 * 60 * 60 * 24 * 2))) // 昨日开始时间（即前天），假如今天2020-09-03，要获取昨日的打卡统计，传给后端的参数（start_time: 2020-09-01, end_time: 2020-09-02）
    let yesterdayEndDate = new Date((timeStamp - (1000 * 60 * 60 * 24))) // 昨日结束时间（即昨天），假如今天2020-09-03，要获取昨日的打卡统计，传给后端的参数（start_time: 2020-09-01, end_time: 2020-09-02）；同时这也是今天开始时间，假如今天2020-09-03，要获取今天的打卡统计，传给后端的参数（start_time: 2020-09-02, end_time: 2020-09-03）
    let todayEndDate = new Date() // 今天结束时间，假如今天2020-09-03，要获取今天的打卡统计，传给后端的参数（start_time: 2020-09-02, end_time: 2020-09-03）

    let yesterdayStartTimeYear = yesterdayStartDate.getFullYear()
    let yesterdayStartTimeMonth = yesterdayStartDate.getMonth() + 1
    yesterdayStartTimeMonth = yesterdayStartTimeMonth < 10 ? ('0' + yesterdayStartTimeMonth) : yesterdayStartTimeMonth
    let yesterdayStartTimeDate = yesterdayStartDate.getDate()
    yesterdayStartTimeDate = yesterdayStartTimeDate < 10 ? ('0' + yesterdayStartTimeDate) : yesterdayStartTimeDate
    
    let yesterdayEndTimeYear = yesterdayEndDate.getFullYear()
    let yesterdayEndTimeMonth = yesterdayEndDate.getMonth() + 1
    yesterdayEndTimeMonth = yesterdayEndTimeMonth < 10 ? ('0' + yesterdayEndTimeMonth) : yesterdayEndTimeMonth
    let yesterdayEndTimeDate = yesterdayEndDate.getDate()
    yesterdayEndTimeDate = yesterdayEndTimeDate < 10 ? ('0' + yesterdayEndTimeDate) : yesterdayEndTimeDate
    
    let todayEndTimeYear = todayEndDate.getFullYear()
    let todayEndTimeMonth = todayEndDate.getMonth() + 1
    todayEndTimeMonth = todayEndTimeMonth < 10 ? ('0' + todayEndTimeMonth) : todayEndTimeMonth
    let todayEndTimeDate = todayEndDate.getDate()
    todayEndTimeDate = todayEndTimeDate < 10 ? ('0' + todayEndTimeDate) : todayEndTimeDate
    
    let yesterdayStartTime = `${yesterdayStartTimeYear}-${yesterdayStartTimeMonth}-${yesterdayStartTimeDate}`
    let yesterdayEndTime = `${yesterdayEndTimeYear}-${yesterdayEndTimeMonth}-${yesterdayEndTimeDate}`
    let todayStartTime = `${yesterdayEndTimeYear}-${yesterdayEndTimeMonth}-${yesterdayEndTimeDate}`
    let todayEndTime = `${todayEndTimeYear}-${todayEndTimeMonth}-${todayEndTimeDate}`


    console.log(yesterdayStartTime, yesterdayEndTime)
    console.log(todayStartTime, todayEndTime)
    
    this.setData({
      yesterdayStartTime,
      yesterdayEndTime,
      todayStartTime,
      todayEndTime,
    })

    this.getYesterdayPunchTheClock()
    this.getTodayPunchTheClock()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let fontSetSize = wx.getStorageSync('fontSetSize') // 本地缓存中用户设置的字体大小
    if (fontSetSize) {
      this.setData({
        fontSetSize
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})